

//  setup.exe key1=123 key2
//  GetCmdValue('key1') return '123'
//  GetCmdValue('key2') return ''
//  GetCmdValue('key3') return ''
function GetCmdValue(key: String): String;
var
cmd_count: Integer;
value: String;
cmd:String;
index: Integer;
nPos: Integer;
begin
  result := '';
  cmd_count := ParamCount();
  for index:=0 to cmd_count do
  begin
    cmd := ParamStr(index);
    nPos := Pos(key, cmd);
    if (nPos = 1) and (Length(cmd) > Length(key)) then
    begin
      value := Copy(cmd, Length(key)+2, Length(cmd)-Length(key)-1);
      result := value;
      exit;
    end;
  end;
end;

//  setup.exe key1=123 key2
//  IsExistCmd('key1') return true;
//  IsExistCmd('key2') return true;
//  IsExistCmd('key3') return fase;
function IsExistCmd(key: String): Boolean;
var
cmd_count: Integer;
cmd:String;
index: Integer;
nPos: Integer;
begin
  result := False;
  cmd_count := ParamCount();
  for index:=0 to cmd_count do
  begin
    cmd := ParamStr(index);
    nPos := Pos(key, cmd);
    if (nPos = 1) then
    begin
      result := True;
      exit;
    end;
  end;
end;

function IsDotNetV4Installed(): Boolean;
var
dotNetV4RegPathClient: String;
dotNetV4RegPathFull: String;
begin
  result := False;
  dotNetV4RegPathClient := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client';
  dotNetV4RegPathFull := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full';
   
  if RegKeyExists(HKLM, dotNetV4RegPathClient) and RegKeyExists(HKLM, dotNetV4RegPathFull) then 
  begin         
    Result := True;
  end
end;

//  file: full path of uexper.ini
//  return ture if succeed
function SetUserExperienceUID(file: String; uid: String): Boolean;
begin

  result := False;
  if FileExists(file) then
  begin
    result := SetIniString('Config', 'SZUID', uid, file);
  end;

end;

////////////////////////////////////////////////// UE ALIYUN BEGIN /////////////////////////////////////////////////
function ExtractAliyunTmpFile(FileName: String): Boolean;
begin
  ExtractTemporaryFile(FileName);
  Result := RenameFile(ExpandConstant('{tmp}\') + FileName, g_aliyuntmp + FileName);
end;

procedure TraceClickSetup();
var
 ResultCode : Integer;
 uepara : String;
begin

//  g_aliyuntmp := GetTempDir() + 'aliyun_' + GetDateTimeString('yyyymmddhhnnss', #0, #0) + '\';
//  CreateDir(g_aliyuntmp);
//
//  ExtractAliyunTmpFile('InfoForSetup.exe');
//  ExtractAliyunTmpFile('AliyunWrapExe.exe');
//  ExtractAliyunTmpFile('AliyunWrap.dll');
//  ExtractAliyunTmpFile('AliyunConfig.ini');
//  ExtractAliyunTmpFile('DataFile.ini');
//
//  uepara := '/SendInfo "Window" "installation" "Activity" "Click_Setup"';
//  Exec(g_aliyuntmp + 'InfoForSetup.exe', uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

procedure TraceClickInstall();
var
 ResultCode : Integer;
 uepara : String;
begin

//  uepara := '/SendInfo "Window" "installation" "Activity" "Click_Install"';
//  Exec(g_aliyuntmp + 'InfoForSetup.exe', uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

procedure TraceClickRetry();
var
 ResultCode : Integer;
 uepara : String;
begin

  uepara := '/SendInfo "Window" "installation" "Activity" "Click_Retry"';
  Exec(g_aliyuntmp + 'InfoForSetup.exe', uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

procedure TraceRegistComFailed();
var
 ResultCode : Integer;
 uepara : String;
begin

//  uepara := '/SendInfo "Window" "installation" "Activity" "Regist_Com_Failed"';
//  Exec(g_aliyuntmp + 'InfoForSetup.exe', uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

procedure TraceInstall();
var
 bAgreeImprove : Boolean;
 ResultCode : Integer;
 uepara : String;
 version : String;
begin

//  uepara := '/SendInfo "Window" "installation" "Activity" "Result_Finish"';
//  Exec(g_aliyuntmp + 'InfoForSetup.exe', uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//
//  if IsExistCmd('agreeImprove') then
//  begin
//    bAgreeImprove := (GetCmdValue('agreeImprove') = 'true');
//  end else
//  begin
//    bAgreeImprove := IsTaskSelected('uexpertask');
//  end;
//  
//  version := '{#SetupSetting("OutputBaseFilename")}';
//  if IsExistCmd('/Recommend') then
//  begin
//    version := version + '-' + GetCmdValue('/Recommend');
//	RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\EaseUS\EDF','Recommend', GetCmdValue('/Recommend'));
//  end;
//
//  uepara := '"{\"Language\":\"' + ExpandConstant('{language}') + '\",\"Version\":\"' + version + '\",\"Version_Num\":\"{#VERSION_NUMBER}\"';
//
//  if IsExistCmd('xurlID') then
//  begin
//    uepara := uepara + ',\"Page_ID\":\"' + GetCmdValue('xurlID') + '\"';
//  end;
//  
//  if IsExistCmd('TestID') then
//  begin
//    uepara := uepara + ',\"Test_ID\":\"' + GetCmdValue('TestID') + '\"';
//  end;
//
//  if bAgreeImprove then
//  begin
//    uepara := '/Enable ' + uepara + ',\"UE\":\"On\"}"';
//  end else
//  begin
//    uepara := '/Disable ' + uepara + ',\"UE\":\"Off\"}"';
//  end; 
//
//  Exec(ExpandConstant('{app}\bin\SetupUE.exe'), uepara, '', SW_HIDE, ewNoWait, ResultCode);
//
//  if g_trace._program_running then
//  begin
//    uepara := '/SendInfo "Window" "ProgramRunning" "Activity" "Click_OK"';
//    Exec(ExpandConstant('{app}\bin\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;
//
//  if g_trace._cover_yes_clicked then
//  begin
//    uepara := '/SendInfo "Window" "CoverInstall" "Activity" "Click_Yes"';
//    Exec(ExpandConstant('{app}\bin\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;
//
//  if g_trace._cover_no_clicked then
//  begin
//    uepara := '/SendInfo "Window" "CoverInstall" "Activity" "Click_No"';
//    Exec(ExpandConstant('{app}\bin\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;  
end;

procedure TraceInstallAbort();
var
 ResultCode : Integer;
 uepara : String;
 ueid:String;
begin
  
//  ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}\InfoForSetup.exe')));
//  ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}\AliyunWrapExe.exe')));
//  ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}\AliyunWrap.dll')));
//  ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}\AliyunConfig.ini')));
//  ExtractTemporaryFile(ExtractFileName(ExpandConstant('{tmp}\DataFile.ini')));
//
//  ueid := GetCmdValue('GUID');
//  if ueid <> '' then
//  begin
//    SetUserExperienceUID(ExpandConstant('{tmp}\AliyunConfig.ini'), ueid);
//  end;
//
//  uepara := '/UninstallStart "' + ExpandConstant('{tmp}') + '"';
//  Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//
//  uepara := '/SendInfo "Window" "' + g_trace._pageTitle + '"';
//
//  if g_trace._isOverInstall then
//  begin
//    uepara := uepara + ' "Source" "Reinstall"';
//  end else
//  begin
//    uepara := uepara + ' "Source" "Install"';
//  end;
//
//  if g_trace._cancel then
//  begin
//    uepara := uepara + ' "Activity" "Click_Cancel"';
//  end;
//
//  Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//
//  if g_trace._program_running then
//  begin
//    uepara := '/SendInfo "Window" "ProgramRunning" "Activity" "Click_OK"';
//    Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;
//
//  if g_trace._cover_yes_clicked then
//  begin
//    uepara := '/SendInfo "Window" "CoverInstall" "Activity" "Click_Yes"';
//    Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;
//
//  if g_trace._cover_no_clicked then
//  begin
//    uepara := '/SendInfo "Window" "CoverInstall" "Activity" "Click_No"';
//    Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//  end;
//
//  uepara := '/UninstallEnd';
//  Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

function IsInstalling(): Boolean;
begin
  result := RegValueExists(HKLM, 'Software\EaseUS\EDF\', 'install_tmp');
end;

procedure TraceUninstall();
var
 ResultCode : Integer;
 uepara : String;
begin
//  RegDeleteValue(HKEY_LOCAL_MACHINE, 'Software\EaseUS\EDF','Recommend');
//  uepara := '/UninstallStart "' + ExpandConstant('{tmp}') + '"';
//  Exec(ExpandConstant('{app}\bin\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
//
//  if IsInstalling() then
//  begin
//    uepara := '/SendInfo "Window" "Uninstall" "Source" "Reinstall" "Activity" "Info_Uninstall"';
//  end else
//  begin
//    uepara := '/SendInfo "Window" "Uninstall" "Source" "Uninstall" "Activity" "Info_Uninstall"';
//  end;
//  Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
// 
//  uepara := '/UninstallEnd';
//  Exec(ExpandConstant('{tmp}\InfoForSetup.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

////////////////////////////////////////// UE ALIYUN END ////////////////////////////////////////////////////////////////////

function RegRootKeyUninstall():String;
begin
  if IsWin64 then
  begin
    Result := 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
  end else
  begin
    Result := 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
  end;
end;

function IsInstalled():Boolean;
var
	szUninstallKey : String;
	asAllNames	: TArrayOfString;
	iNameCount	: Integer;
	szKeyName		: String;
  oldName : string;
begin
	Result := False;

	if RegGetSubkeyNames(HKEY_LOCAL_MACHINE, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall', asAllNames) then
	begin
   		for iNameCount := 0 to GetArrayLength(asAllNames) - 1 do
	   	begin
	        szKeyName := asAllNames[iNameCount];
			oldName := Uppercase(szKeyName);
 	        if ((Pos('XPower XFastCopy_is1', oldName) > 0)) then
 	        begin
            	Result := True;
 	        end;
		end;
	end;
	if false = Result then
	begin
		if RegGetSubkeyNames(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', asAllNames) then
		begin
			for iNameCount := 0 to GetArrayLength(asAllNames) - 1 do
			begin
				szKeyName := asAllNames[iNameCount];
				oldName := Uppercase(szKeyName);
				if ((Pos('XPower XFastCopy_is1', oldName) > 0)) then
				begin
					Result := True;
				end;
			end;
		end;
	end;
end;

function IsAppRunning(const FileName : string): Boolean;
var
    FSWbemLocator: Variant;
    FWMIService   : Variant;
    FWbemObjectSet: Variant;
begin
    Result := false;
    FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');
    FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
    FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));
    Result := (FWbemObjectSet.Count > 0);
    FWbemObjectSet := Unassigned;
    FWMIService := Unassigned;
    FSWbemLocator := Unassigned;
end;

function Is_be10_32() : Boolean;
var
    Version   :TWindowsVersion;
begin
  GetWindowsVersionEx(Version);
  result := ((Version.Major >= 10) and (not IsWin64));
end;

function Is_be10_64() : Boolean;
var
    Version   :TWindowsVersion;
begin
  GetWindowsVersionEx(Version);
  result := ((Version.Major >= 10) and (IsWin64));
end;

function Is5_2() : Boolean;
var
    Version: TWindowsVersion;
begin
     GetWindowsVersionEx(Version);
     Result := ((Version.Major = 5) and (Version.Minor <= 2));
end;


procedure InstallENS();
var
 ResultCode : Integer;
 uepara : String;
begin

//  uepara := '-install "{app}\bin\ens" "MU-WPR-TRIL-WIN" "1" "{app}\bin\XCalculator.exe" {#VERSION_NUMBER}';
//  Exec(ExpandConstant('{app}\bin\ens\EnsUtils.exe'), ExpandConstant(uepara), '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;

procedure UninstallENS();
var
 ResultCode : Integer;
 uepara : String;
begin

//  uepara := '-uninstall "MU-WPR-TRIL-WIN"';
//  Exec(ExpandConstant('{app}\bin\ens\EnsUtils.exe'), uepara, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

end;
